#include "noticewindow.h"
#include "ui_noticewindow.h"
#include <QJsonDocument>
#include "mytcpsocket.h"

NoticeWindow::NoticeWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::NoticeWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("通知");
    setQssStyle(QString("../style/NoticeWindow.qss"),this);
    // 无边框
    this->setWindowFlags(Qt::FramelessWindowHint);
    //隐藏滚动条
    //ui->listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //垂直滚动条
    ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //水平滚动条

    connect(this,SIGNAL(sign_dealResult(QVariant)),this,SLOT(updateDealResultNotice(QVariant)));

    connect(this,SIGNAL(sign_addNotice(QVariant)),this,SLOT(slot_addNotice(QVariant)));

    ui->btn_clear->setIcon(QIcon(QPixmap(":/prefix2/software/clean.png")));

}

NoticeWindow::~NoticeWindow()
{
    delete ui;
}

void NoticeWindow::updateNoticeIcon()
{
    //无论窗口是否显示,只要还有没处理的消息,就认为有未读消息
    if(ui->listWidget->count() == 0)
        emit noticeIconStatus(0);
    else
        emit noticeIconStatus(1);
}

void NoticeWindow::addNotice(struct st_notice notice)
{
    //vec_notice.push_back(notice);

    QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
    item->setSizeHint(QSize(ui->listWidget->width(),70));  //设置大小(主要是行高)
    ui->listWidget->addItem(item);
    //设置角色数据(隐藏数据)
    QVariant var;
    var.setValue(notice);
    item->setData(Qt::UserRole,var);
    NoticeItem* m_notice = new NoticeItem(notice,this);
    ui->listWidget->setItemWidget(item,m_notice);
    connect(m_notice,SIGNAL(agreeOrRefuse(int,QVariant)),this,SLOT(dealOtherAddFriend(int,QVariant)));

    //更新UI界面图标
    updateNoticeIcon();
}

void NoticeWindow::deleteNotice(int uid,int type)
{
    for(int i = 0;i<ui->listWidget->count();i++){
        QVariant var = ui->listWidget->item(i)->data(Qt::UserRole);
        struct st_notice notice = var.value<struct st_notice>();
        if(notice.fdata.dd_num == uid && notice.type == type){
            ui->listWidget->removeItemWidget(ui->listWidget->item(i));  //只删除Item上的Widget,没有删除Item
            delete ui->listWidget->item(i);
            break;
        }
    }
    //更新UI界面图标
    updateNoticeIcon();
}


void NoticeWindow::dealOtherAddFriend(int flag,QVariant var)
{
    struct st_notice notice = var.value<st_notice>();
    //获取自己 和 好友的dd_num
    int user = GlobalObject::getGlobalObject()->getUserInfo().sdata.dd_num;

    //封装登录请求
    QVariantMap map;
    map["cmd"]="add_friend_deal_result";
    map["send"]=notice.fdata.dd_num;        //发送请求的是好友
    map["recv"]=user;                       //处理请求的是自己

    //拒绝对方
    if(flag == 0){
        map["result"]="refuse";
    }
    //同意对方
    else if(flag == 1){
        map["result"]="agree";
    }
    //直接删除
    else if(flag == 2){
        //从信箱删除
        deleteNotice(notice.fdata.dd_num,notice.type);
        return;
    }

    //发送添加好友处理结果
    GlobalObject::getGlobalObject()->sendRequest(map);

    //处理完毕,从信箱删除
    deleteNotice(notice.fdata.dd_num,notice.type);

}

void NoticeWindow::updateDealResultNotice(QVariant var)
{
    // QVariant -> struct st_notice
    struct st_notice notice = var.value<struct st_notice>();
    //通知加入通知信箱
    addNotice(notice);
}

void NoticeWindow::slot_addNotice(QVariant var)
{
    struct st_notice notice = var.value<struct st_notice>();
    addNotice(notice);
}


void NoticeWindow::on_btn_close_clicked()
{
    this->close();
}


void NoticeWindow::on_btn_min_clicked()
{
    this->showMinimized();
}


void NoticeWindow::on_btn_clear_clicked()
{
    ui->listWidget->clear();
    //更新UI界面图标
    updateNoticeIcon();
}

